from typing import List

import product as product
import account as account
import cardBatchTmp as cardBatchTmp


def handAllianceCardV2(account_info: account.AccountInfo, products: List[product.ProductInfo]) -> None:
    if not account_info:
        print("accountId is null，添加选品车提前退出")
        return
    if not products:
        print("商品为空，添加选品车提前退出")
        return

    display_id = account_info.display_id
    open_id = account_info.open_id
    access_token = account_info.access_token
    user_id = account_info.user_id

    # 商品数据分组，20个为一组，接口一次最多20个商品
    grouped_products = [products[i:i + 20] for i in range(0, len(products), 20)]
    for creator_products in grouped_products:
        product_list = [
            {"activity_url": product.url, "product_id": product.product_id}
            for product in creator_products
        ]
        cardBatchTmp.cardBatchTmp(user_id, display_id, open_id, access_token, product_list)


if __name__ == "__main__":
    # 获取账号信息
    accounts = account.get_accounts()
    # 获取商品信息
    products = product.generateProducts()
    current = 0
    for account_info in accounts:
        try:
            handAllianceCardV2(account_info, products)
            current += 1
            print(f"当前位于: {current} 一共有 {len(accounts)} 个账号")
        except Exception as e:
            print(f"displayId:{account_info} error:{str(e)}")
